package com.xiaoq.social.repository.impl;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.social.entity.FollowEntity;
import com.xiaoq.social.repository.IFollowRepository;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class FollowReposityImpl implements IFollowRepository{
	
	@Autowired
	private Datastore datastore;
	
	@Autowired
	private IdGenerator idGenerator;
	
	private static final Logger LOG = LoggerFactory.getLogger(FollowReposityImpl.class);

	@Override
	public boolean isFollow(String tgtId, String userId) {
		Query<FollowEntity> followQuery = datastore.createQuery(FollowEntity.class);
		if(StringUtils.isNotEmpty(tgtId)&&StringUtils.isNotEmpty(userId)){
			FollowEntity follow = followQuery.field("userId").equal(userId).field("targetId").equal(tgtId).get();
			if(follow!=null){
				LOG.info(userId+"已经关注"+tgtId);
				return true;
			}else{
				LOG.info(userId+"未关注"+tgtId);
			}
			
		}else{
		LOG.error("tgtId或者userId不能为空会报空指针异常！");
		}
		return false;
	}

	@Override
	public boolean addFollow(FollowEntity follow) {
		if(follow!=null){
			String id = idGenerator.nextId();
			follow.setId(id);
			LOG.info("FollowEntity实体类的Id！");
			datastore.save(follow);
			return true;
		}else{
			LOG.error("FollowEntity 实体为空！");
		}
		
		return false;
	}

	@Override
	public boolean delFollow(String userId, String tgtId) {
		Query<FollowEntity> followQuery = datastore.find(FollowEntity.class);
		if (StringUtils.isNoneEmpty(userId) && StringUtils.isNoneEmpty(tgtId)) {
			followQuery.field("targetId").equal(tgtId).field("userId").equal(userId);
			if (followQuery != null) {
				datastore.delete(followQuery);
				return true;
			}
		}
		LOG.error("Follow删除失败！");
		return false;
	}

	@Override
	public Long findFollowNumsByUser(String userId) {
		Query<FollowEntity> followQuery = datastore.find(FollowEntity.class);
		Long followNums = 0L;
		if(StringUtils.isNotEmpty(userId)){
			followQuery.field("userId").equal(userId);
			followNums = followQuery.count();
			
		}
		LOG.info("查询用户收藏数量："+followNums);
		return followNums;
	}

	@Override
	public Long findFollowNumsByTgt(String tgtId) {
		Query<FollowEntity> followQuery = datastore.find(FollowEntity.class);
		Long followNums = 0L;
		if(StringUtils.isNotEmpty(tgtId)){
			followQuery.field("targetId").equal(tgtId);
			followNums = followQuery.count();
			
		}
		LOG.info("查询目标被收藏数量："+followNums);
		return followNums;
	}

	
	@Override
	public List<FollowEntity> findFollowListByUser(String userId,PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Query<FollowEntity> followQuery = datastore.find(FollowEntity.class);
		Long total = 0L;
		List<FollowEntity> followList = new ArrayList<FollowEntity>();
		if(StringUtils.isNotEmpty(userId)){
			followQuery.field("userId").equal(userId);
			total = followQuery.count();
			followQuery.order("-createdAt").offset(pn).limit(ps);
			followList = followQuery.asList();
			
		}
		pvo.setTc(total);
		return followList;
	}

	@Override
	public List<FollowEntity> findFollowListByTargetIdList(String userId, List<String> targetIdList, String targetType) {
		if(CollectionUtils.isEmpty(targetIdList) || StringUtils.isAnyBlank(userId, targetType)){
			return new ArrayList<>();
		}
		Query<FollowEntity> followQuery = datastore.find(FollowEntity.class)
				.field("userId").equal(userId)
				.field("targetId").in(targetIdList)
				.field("targetType").equal(targetType);
		return followQuery.asList();
	}
}
